package com.lxg.controller.booksController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxg.pojo.Books;
import com.lxg.pojo.Borrow;
import com.lxg.pojo.User;
import com.lxg.service.booksService.BooksShowService;
import com.lxg.service.userService.UserBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class BooksController {
    @Autowired
    BooksShowService booksShowService;

    @Autowired
    UserBorrowService userBorrowService;

    /**
     * 按照订阅次数排序
     * 查询全部书本
     * @return
     */



    @RequestMapping("/")
    public String showBooks(Model model,HttpSession session,HttpServletRequest request,@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){

        String addResult = request.getParameter("addResult");
        if (addResult!=null ){
            if( addResult.equals("true") || addResult.equals("false")) {
                request.setAttribute("addResult", addResult);
            }
        }

        String borrowResult = request.getParameter("borrowResult");
        if (borrowResult!=null ){
            if(borrowResult.equals("true") || borrowResult.equals("false")) {
                request.setAttribute("borrowResult",borrowResult);
            }
        }


        String maxResult = request.getParameter("maxResult");
        if(maxResult!=null){
            if ( maxResult.equals("true") || maxResult.equals("false")){
                request.setAttribute("maxResult",maxResult);
            }
        }


        //获取当前用户
        User user = (User) session.getAttribute("user");

        if(user!=null){
            Integer uid = user.getUid();
            //根据uid查找书架信息
            ArrayList<Borrow> borrows = userBorrowService.showMyBooks(uid);
            if(borrows.size()>0){
                session.setAttribute("myBooksSize",borrows.size());
                session.setAttribute("noEmpty","true");
            }else{
                session.setAttribute("myBooksSize",borrows.size());
                session.setAttribute("isEmpty","true");
            }
        }else{
            session.setAttribute("myBooksSize",0);
            session.setAttribute("isEmpty","true");
        }

        PageHelper.startPage(pageNum,pageSize);
        ArrayList<Books> allBooks = booksShowService.getAllBooks();
        PageInfo<Books> pageInfo = new PageInfo<>(allBooks, 3);
        System.out.println(pageInfo.getList().size());
        model.addAttribute("pageInfo",pageInfo);
//        model.addAttribute("searchText","");
        return "/index";
    }



    //普通请求实现方式
    /*@RequestMapping("/")
    public String showBooks(Model model,HttpSession session,HttpServletRequest request,@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){

        String searchText = request.getParameter("searchText");

        System.out.println("11"+searchText);


        PageInfo<Books> pageInfo = null;

        if (searchText=="" || searchText==null){
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Books> allBooks = booksShowService.getAllBooks();
            pageInfo = new PageInfo<>(allBooks,3);
            System.out.println(pageInfo.getList().size());
            model.addAttribute("pageInfo",pageInfo);

//            request.setAttribute("searchText","");

            return "/index";
        }else{

            System.out.println("test:"+searchText);

            request.setAttribute("searchText",searchText);

            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Books> searchNameBooks = booksShowService.getAllLikeBooksByName(searchText);
            ArrayList<Books> searchBooks = searchNameBooks;

            if (searchNameBooks.size()==0){
                PageHelper.startPage(pageNum,pageSize);
                ArrayList<Books> searchAuthorBooks = booksShowService.getAllLikeBooksByAuthor(searchText);
                searchBooks = searchAuthorBooks;
            }


            pageInfo = new PageInfo<>(searchBooks,3);
            System.out.println(pageInfo.getList().size());
            model.addAttribute("pageInfo",pageInfo);
            return "/index";
        }
    }*/

    /**
     * 根据图书bid查询唯一对应图书
     * @return
     */
    @RequestMapping("getBookByBid")
    public String getBookByBid(Integer bid,HttpSession session){
        Books bookByBid = booksShowService.getBookByBid(bid);
        session.setAttribute("bookByBid",bookByBid);
        return "/index";
    }

    @RequestMapping("getAllLikeBooksByBid")
    public String getAllLikeBooksByBid(Integer bid , HttpSession session){
        ArrayList<Books> allLikeBooksByBid = booksShowService.getAllLikeBooksByBid(bid);
        session.setAttribute("allLikeBooksByBid",allLikeBooksByBid);
        return "/index";
    }

    @RequestMapping("getAllLikeBooksByName")
    public String getAllLikeBooksByName(String name , HttpSession session){
        ArrayList<Books> allLikeBooksByName =
                booksShowService.getAllLikeBooksByName(name);
        session.setAttribute("allLikeBooksByName",allLikeBooksByName);
        return "/index";
    }


    @RequestMapping("getAllLikeBooksByAuthor")
    public String getAllLikeBooksByAuthor(String author , HttpSession session){
        ArrayList<Books> allLikeBooksByAuthor =
                booksShowService.getAllLikeBooksByAuthor(author);
        session.setAttribute("allLikeBooksByAuthor",allLikeBooksByAuthor);
        return "/index";
    }





}
